package com.my.unsort;

import com.my.domain.OrderStep;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;
//演示消息顺序错乱
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        producer.setNamesrvAddr("localhost:9876");
        producer.start();

        List<Message> msgList = generateMsgList();
        for (Message message : msgList) {
            SendResult sendResult = producer.send(message);
            System.out.println(sendResult);
        }
        producer.shutdown();
    }

    private static List<Message> generateMsgList(){//模拟按照顺序生成5个订单的相关步骤
        List<Message> msgList = new ArrayList<>();
        String[] descs = {"创建","付款","推送","完成"};
        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < 4; j++) {
                OrderStep orderStep = new OrderStep(i,descs[j]);
                msgList.add(new Message("topic11", "tag1", orderStep.toString().getBytes()));
            }
        }
        return msgList;
    }
}
